<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use think\facade\Db;
use think\facade\View;

class Message extends BaseController
{
    public function index()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';
        $messages=Db::name('Message')->where(['to_uid'=>$uid, 'is_read'=>0])->select();
        $res=Db::name('Message')->where(['to_uid'=>$uid, 'is_read'=>0])->save(['is_read'=>1]);
        //dd($messages);
       // add_user_log('view', '个人中心');
        View::assign('userInfo', $userInfo);
        View::assign('messages', $messages);

        return view();
    }
    /**
     * 已读消息查阅
     * */
    public function is_read_message()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';
        $messages=Db::name('Message')->where(['to_uid'=>$uid,'is_read'=>1])->select();
        //dd($messages);
       // add_user_log('view', '个人中心');
        View::assign('userInfo', $userInfo);
        View::assign('messages', $messages);

        return view();
    }

    /**
     * 所有消息查阅
     * */
    public function all_message()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';
        $messages=Db::name('Message')->where(['to_uid'=>$uid])->select();
        //dd($messages);
       // add_user_log('view', '个人中心');
        View::assign('userInfo', $userInfo);
        View::assign('messages', $messages);

        return view();
    }
    


}
